package com.qs.worklog.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.qs.worklog.dao.UserDAO;
import com.qs.worklog.dao.impl.ImplUserDAO;
import com.qs.worklog.entity.User;

public class LoginServlet  extends HttpServlet{
	private static final long serialVersionUID = 1L;

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String name = (request.getParameter("name")).trim();
		UserDAO userDao = new ImplUserDAO();
		User user = userDao.findUserByName(name);
		if(user != null){
			HttpSession session = request.getSession();
			session.setAttribute("user", user);
			List<User> users = userDao.findAllUser();
			request.setAttribute("users", users);
			response.sendRedirect("home.do");
		}else{
			request.setAttribute("msg", "User isn't Existing");
			request.getRequestDispatcher("login.jsp").forward(request, response);
		}
	}
}
